package com.yx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.entity.User;
import com.yx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname UserController
 * @Date 2021/07/06 08:26
 * @Author by yx
 * @Description TODO:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @PostMapping("/addUser")
    public Boolean addUser(@RequestBody User user) {
//        user.setId(""); // 当前登录的id
        user.setCreateTime(new Date());
        return userService.save(user);
    }

    @GetMapping("/getUserList")
    public List<User> getUserList() {
        List<User> list = userService.list();
        System.out.println(list);
        return list;
    }

    @PostMapping("/queryUserList")
    public List<User> queryUserList(@RequestBody User user) {

        // 1.创建一个查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(user.getUsername())) {
            wrapper.like("username", user.getUsername());
        }
        if (!StringUtils.isEmpty(user.getEmail())) {
            wrapper.eq("email", user.getEmail());
        }
        if (!StringUtils.isEmpty(user.getPhone())) {
            wrapper.eq("phone", user.getPhone());
        }

        // 2.根据查询条件查询数据
        return userService.list(wrapper);
    }

    @GetMapping("/updateUser/{id}")
    public User updateUser(@PathVariable("id") Integer id) {
        User user = userService.getById(id);

        return user;
    }

    @PostMapping("/updateUser")
    @ResponseBody
    public Boolean updateUser(@RequestBody User user) { // 这里接收的是VO，主要是表单传递过来
        //设置数据修改时间
        user.setUpdateTime(new Date());
        //设置修改人的id
//        user.setUpdateUser();
        Boolean trager = userService.updateById(user);
        return trager;
    }

    @PostMapping("/deleteUser")
    public boolean deleteUsers(@RequestBody int[] ids) {
        List list = new ArrayList();
        for (int id : ids) {
            list.add(id);
        }
        boolean b = userService.removeByIds(list);
        return b;
    }

    @GetMapping("/deleteUserById/{id}")
    public Boolean deleteUserById(@PathVariable("id") Integer id){
        return userService.removeById(id);
    }

    @GetMapping("/getUserByName")
    public User getUserByName(@RequestParam("username") String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userService.getOne(queryWrapper);
    }


    @GetMapping("/getUserByOauthId")
    public User getUserByOauthId(@RequestParam("uuid") String uuid){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("oauth_id", uuid);
        return userService.getOne(queryWrapper);
    }
}
